package com.ly.hotel.spainterface.jxd.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.ly.hotel.R;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.spainterface.jxd.activity.JxdXiaoFeiActivity;
import com.ly.hotel.spainterface.nm.bean.NmXFBuyItemBean;
import com.ly.hotel.spainterface.nm.event.XFCartOperationCartEvent;
import com.ly.hotel.util.ImageUtil;

import java.util.List;

import de.greenrobot.event.EventBus;

public class JxdXFShopCartAdapter extends BaseAdapter {
	JxdXiaoFeiActivity mContext;
	LayoutInflater mInflater;
	private List<NmXFBuyItemBean> xtCartList;
	private int rod;
	private String moneyUint;

	public JxdXFShopCartAdapter(JxdXiaoFeiActivity c, List<NmXFBuyItemBean> xtCartList) {
		this.xtCartList = xtCartList;
		mContext = c;
		mInflater = LayoutInflater.from(mContext);
		rod = mContext.getResources().getDimensionPixelSize(R.dimen.px9);
		moneyUint = mContext.getResources().getString(R.string.xf_rmb);
	}

	@Override
	public int getCount() {
		return xtCartList.size();
	}

	@Override
	public Object getItem(int postion) {
		return xtCartList.get(postion);
	}

	@Override
	public long getItemId(int postion) {
		return postion;
	}
	
	@Override
	public View getView(int postion, View contentView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		final int pos = postion;
		ViewHolder holder;
		if (contentView == null) {
			holder = new ViewHolder();
			contentView = mInflater.inflate(R.layout.xf_shop_cart_item, null);
			holder.mpic = (ImageView) contentView.findViewById(R.id.cart_list_img);
			holder.myname = (TextView) contentView.findViewById(R.id.cart_list_name);
			holder.myaction = (TextView) contentView.findViewById(R.id.cart_list_action);
			holder.mysub = (ImageButton) contentView.findViewById(R.id.cart_list_sub);
			holder.mycount = (TextView) contentView.findViewById(R.id.cart_list_count);
			holder.myadd = (ImageButton) contentView.findViewById(R.id.cart_list_add);
			holder.myremove = (ImageButton) contentView.findViewById(R.id.cart_list_remove);
		} else {
			holder = (ViewHolder) contentView.getTag();
		}

		String imgPath = MyApplication.getHostResUrl() + "/upload/interface/consume/pic/"
				+ xtCartList.get(postion).getId() + ".jpg";
		ImageUtil.displayImage_Round(mContext,imgPath,holder.mpic,rod);
		holder.myname.setText(xtCartList.get(postion).getName() + "\n" + xtCartList.get(postion).getPrice() +
						moneyUint + " / " + xtCartList.get(postion).getUnit());
		holder.myaction.setText(xtCartList.get(postion).getAction());
		holder.mycount.setText(xtCartList.get(postion).getNum() + "");

		holder.mysub.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				EventBus.getDefault().post(new XFCartOperationCartEvent(pos,1));
			}
		});

		holder.myadd.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				EventBus.getDefault().post(new XFCartOperationCartEvent(pos,2));
			}
		});

		holder.myremove.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				EventBus.getDefault().post(new XFCartOperationCartEvent(pos,3));
			}
		});
		contentView.setTag(holder);
		return contentView;
	}

	static class ViewHolder {
		ImageView mpic;
		TextView myname;
		TextView myaction;
		ImageButton mysub;
		TextView mycount;
		ImageButton myadd;
		ImageButton myremove;
	}
}
